// Define namespace

Common.Api = {}


// Constants

Common.Api.FORMAT_JSON = 'json';
Common.Api.FORMAT_XML = 'xml';


/**
 *
 */

Common.Api.call = function(extensionId, objectName, apiName, apiArguments, outputFormat, callbackSuccess, callbackFailure, hideNotification)
{
	// Build the URL
	
	var url = '<?php p(root(false)); ?>';
	
	if
	(
		(extensionId != null) &&
		(extensionId.length > 0)
	)
	{
		url += '[' + extensionId + ']/';
	}
	
	url += 'api/' + outputFormat + '/' + objectName + '.' + apiName;
	
	
	// Post arguments to the URL
	 
	$.post
	(
		url,
		apiArguments,
		function(rawResult, status)
		{
			Common.Api.onApiResult
			(
				url,
				rawResult,
				status,
				outputFormat,
				callbackSuccess,
				callbackFailure,
				hideNotification,
				false
			);
		}
	);
}


/**
 *
 */

Common.Api.callJson = function(extensionId, objectName, apiName, apiArguments, callbackSuccess, callbackFailure, hideNotification)
{
	Common.Api.call
	(
		extensionId,
		objectName,
		apiName,
		apiArguments,
		Common.Api.FORMAT_JSON,
		callbackSuccess,
		callbackFailure,
		hideNotification
	);
}


/**
 *
 */

Common.Api.callXml = function(extensionId, objectName, apiName, apiArguments, callbackSuccess, callbackFailure, hideNotification)
{
	Common.Api.call
	(
		extensionId,
		objectName,
		apiName,
		apiArguments,
		Common.Api.FORMAT_XML,
		callbackSuccess,
		callbackFailure,
		hideNotification
	);
}


/**
 *
 */

Common.Api.onApiResult = function(url, rawResult, status, outputFormat, callbackSuccess, callbackFailure, hideNotification, acceptString)
{
	// Do nothing if not 'success' yet
	
	if (status != 'success')
	{
		return;
	}
	
	
	// Convert the raw result into the end format
	
	var result = rawResult;
	
	if (outputFormat == Common.Api.FORMAT_XML)
	{
		result = xml2json.parser(rawResult);
	}
	
	
	// We may have received an exception, grab it
	
	var errorCode = null;
	var errorContext = null;
	var errorDescription = null;
	var errorFile = null;
	var errorLine = null;
	var errorTitle = null;
	
	if (typeof(result) == 'object')
	{
		try
		{
			errorDescription = result['errorDescription'];
			errorTitle = result['errorTitle'];
			
			errorCode = result['errorCode'];
			errorContext = result['errorContext'];
			errorFile = result['errorFile'];
			errorLine = result['errorLine'];
		}
		catch (e)
		{
		}
	}
	else if
	(
		(acceptString != true) &&
		(typeof(result) == 'string')
	)
	{
		errorContext = new String();
		console.log(result);
		for (i in result)
		{
			errorContext += result[i];
		}
		
		Ui_Notification.showError
		(
			'<?php p(str(EXCEPTION_API_RESULT_NOT_VALID)); ?>',
			"<?php p(str(EXCEPTION_API_RESULT_NOT_VALID . '?description')); ?>",
			null,
			null,
			null,
			errorContext
		);

		return;
	}
	
	
	// So, did it work?
	
	if (errorCode == null)
	{
		// If a callback for success was defined, call it
		
		if (callbackSuccess != null)
		{
			callbackSuccess(result, url);
		}
	}
	else
	{
		// Show a notification

		if (hideNotification != true)
		{
			Ui_Notification.showError(errorTitle, errorDescription, errorCode, errorFile, errorLine, errorContext);
		}
		
		
		// If a callback for failure was defined, call it
		
		if (callbackFailure != null)
		{
			callbackFailure(result, errorTitle, errorDescription, errorCode, errorFile, errorLine, errorContext);
		}
	}
}


/**
 * 
 */

Common.Api.renderView = function(extensionId, pathToView, viewArguments, targetId, outputFormat, callbackSuccess, callbackFailure, hideNotification)
{
	// Build the URL of the view
	
	var url = '<?php p(root(false)); ?>';
	
	if (extensionId != null)
	{
		url += '[' + extensionId + ']/';
	}
	
	url += 'view' + pathToView;
	
	
	// Post arguments to the URL
	 
	$.post
	(
		url,
		viewArguments,
		function(rawResult, status)
		{
			Common.Api.onApiResult
			(
				url,
				rawResult,
				status,
				outputFormat,
				function(result)
				{
					// Replace the content of the target
			
					if (targetId != null)
					{
						$('#' + targetId).html(rawResult);
					}


					// If a callback for success was defined, call it
					
					if (callbackSuccess != null)
					{
						callbackSuccess(result, url);
					}
				},
				callbackFailure,
				hideNotification,
				true
			);
		}
	);	
}
